package com.xiaolei.lib.core;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;

import com.xiaolei.lib.Activity.IActivity;
import com.xiaolei.lib.Activity.bean.AnnotationWrape;
import com.xiaolei.lib.annotation.AutoWrite;
import com.xiaolei.lib.annotation.AutoWriteView;
import com.xiaolei.lib.annotation.Bind;
import com.xiaolei.lib.annotation.OnActivityResult;
import com.xiaolei.lib.annotation.findViewById;
import com.xiaolei.lib.annotation.getAction;
import com.xiaolei.lib.annotation.getAll;
import com.xiaolei.lib.annotation.getBoolean;
import com.xiaolei.lib.annotation.getColor;
import com.xiaolei.lib.annotation.getDrawable;
import com.xiaolei.lib.annotation.getFloat;
import com.xiaolei.lib.annotation.getInt;
import com.xiaolei.lib.annotation.getIntent;
import com.xiaolei.lib.annotation.getLong;
import com.xiaolei.lib.annotation.getSerializable;
import com.xiaolei.lib.annotation.getSharedPreferences;
import com.xiaolei.lib.annotation.getString;
import com.xiaolei.lib.annotation.getStringArray;
import com.xiaolei.lib.annotation.getStringSet;
import com.xiaolei.lib.annotation.setContentView;
import com.xiaolei.lib.annotation.setOnClick;
import com.xiaolei.lib.annotation.setOnItemClick;
import com.xiaolei.lib.annotation.factory.TranslatorFactory;
import com.xiaolei.lib.annotation.translators.*;

/**
 * 解释器 - 的核心调度类
 * 
 * @author xiaolei
 *
 */
public class TranslatorCore extends HashMap<AnnotationWrape, ITranslator>
{
	private static final long serialVersionUID = 7538599254697368385L;
	private static TranslatorCore factory;

	private TranslatorCore()
	{
		// 将一些默认的注解，以及对应的解析器，注册到解析器工厂中
		Regist(BindTranslator.class, Bind.class);
		Regist(AutoWriteTranslator.class, AutoWrite.class);
		Regist(getIntentTranslator.class, getIntent.class);
		Regist(getIntent_getString_Translator.class, getIntent.getString.class);
		Regist(getIntent_getStringArray_Translator.class, getIntent.getStringArray.class);
		Regist(getIntent_getSerializableExtra_Translator.class, getIntent.getSerializableExtra.class);
		Regist(getIntent_getInt_Translator.class, getIntent.getInt.class);
		Regist(getIntent_getBoolean_Translator.class, getIntent.getBoolean.class);
		Regist(getIntent_getAction_Translator.class, getIntent.getAction.class);
		Regist(getColorTranslator.class, getColor.class);
		Regist(getDrawableTranslator.class, getDrawable.class);
		Regist(OnActivityResultTranslator.class, OnActivityResult.class);
		Regist(setOnItemClickTranslator.class, setOnItemClick.class);
		Regist(SharedPreferencesTranslator.class, getSharedPreferences.class);
		Regist(AutoWriteViewTranslator.class, AutoWriteView.class);
		registExt();
	}

	/**
	 * 注册扩展的解释器
	 */
	private void registExt()
	{
		Regist(FindViewByIdTranslator.class, findViewById.class);
		Regist(setContentViewTranslator.class, setContentView.class);
		Regist(setOnclickTranslator.class, setOnClick.class);
		//Intent
		Regist(getIntent_getString_Translator2.class, getIntent.class, getString.class);
		Regist(getIntent_getAction_Translator2.class, getIntent.class, getAction.class);
		Regist(getIntent_getBoolean_Translator2.class, getIntent.class, getBoolean.class);
		Regist(getIntent_getInt_Translator2.class, getIntent.class,getInt.class);
		Regist(getIntent_getStringArray_Translator2.class, getIntent.class,getStringArray.class);
		Regist(getIntent_getSerializableExtra_Translator2.class, getIntent.class,getSerializable.class);
		//SharedPreferences
		Regist(getSharedPreferences_getBoolean_Translator.class, getSharedPreferences.class,getBoolean.class);
		Regist(getSharedPreferences_getFloat_Translator.class, getSharedPreferences.class,getFloat.class);
		Regist(getSharedPreferences_getInt_Translator.class, getSharedPreferences.class,getInt.class);
		Regist(getSharedPreferences_getLong_Translator.class, getSharedPreferences.class,getLong.class);
		Regist(getSharedPreferences_getString_Translator.class, getSharedPreferences.class,getString.class);
		Regist(getSharedPreferences_getStringSet_Translator.class, getSharedPreferences.class,getStringSet.class);
		Regist(getSharedPreferences_getAll_Translator.class, getSharedPreferences.class,getAll.class);
		//自动根据同名称注入相应ID的View
		Regist(Bind_AutoWriteView_Translator.class, Bind.class,AutoWriteView.class);
		Regist(setContentView_AutoWriteView_Translator.class, setContentView.class,AutoWriteView.class);
		
	}

	public static TranslatorCore getInstance()
	{
		synchronized (TranslatorCore.class)
		{
			if (factory == null)
			{
				factory = new TranslatorCore();
			}
		}
		return factory;
	}

	/**
	 * 在这里注册 自定义注解的解析器
	 * 
	 * @param annotation
	 * @param translator
	 */
	public synchronized void Regist(Class<? extends ITranslator> translator, Class<?>... annotations)
	{
		@SuppressWarnings("unchecked")
		AnnotationWrape wrape = new AnnotationWrape((Class<? extends Annotation>[]) annotations);
		this.put(wrape, TranslatorFactory.getTranslator(translator));
	}

	/**
	 * 反注册（取消注册）自定义注解的解析器
	 * 
	 * @param annotation
	 */
	public synchronized void unRegist(Class<? extends Annotation> annotation)
	{
		this.remove(annotation);
	}

	/**
	 * 根据某一个(组)注解，寻找相应的注解解析器
	 * 
	 * @param annotation
	 * @return
	 */
	public ITranslator geTranslator(AnnotationWrape annotation)
	{
		return this.get(annotation);
	}
	
	/**
	 * 依次解析 类，变量，方法。
	 * 
	 * @param activity
	 */
	public void initAnotationActivity(IActivity activity)
	{
		Class<? extends IActivity> clazz = activity.getClass();
		initAnotationType(activity, clazz);
		initAnotationFields(activity, clazz);
		initAnotationMethods(activity, clazz);
	}

	/**
	 * 解析類的註解，比如Activity的註解，多用於設置上下文
	 * 
	 * @param activity
	 * @param clazz
	 */
	public void initAnotationType(IActivity activity, Class<? extends IActivity> clazz)
	{
		Annotation annotations[] = clazz.getAnnotations();
		if (annotations != null)
		{
			@SuppressWarnings("unchecked")
			Class<? extends Annotation> annotation_class_s[] = new Class[annotations.length];
			for (int j = 0; j < annotations.length; j++)
			{
				Annotation annotation = annotations[j];
				Class<? extends Annotation> annotation_class = annotation.annotationType();
				annotation_class_s[j] = annotation_class;
			}
			/**
			 * 这里以后将扩展，将支持更加灵活的注解方式，好几种组合方式， 以便一个 Class 一个 Methed
			 * 一个Field通过的不同的组合，达到不同的意义，实现更加灵活的配置
			 */
			// HashSet<AnnotationWrape> set =
			// AnnotationCombination.Parse(annotation_class_s);
			// for (AnnotationWrape annotationWrape : set)
			// {
			AnnotationWrape annotationWrape = new AnnotationWrape(annotation_class_s);
			ITranslator translator = geTranslator(annotationWrape);

			for (Class<? extends Annotation> class1 : annotationWrape)
			{
				annotationWrape.put(class1, clazz.getAnnotation(class1));
			}

			if (translator != null)
				translator.execute(clazz, activity, annotationWrape);
			// }
		}
	}

	/**
	 * 解析成员变量的注解
	 * 
	 * @param activity
	 * @param clazz
	 */
	public void initAnotationFields(IActivity activity, Class<? extends IActivity> clazz)
	{
		Field fields[] = clazz.getDeclaredFields();// 獲取所有成員
		for (int i = 0; i < fields.length; i++)
		{
			Field field = fields[i];
			Annotation annotations[] = field.getDeclaredAnnotations();
			if (annotations != null)
			{
				@SuppressWarnings("unchecked")
				Class<? extends Annotation> annotation_class_s[] = new Class[annotations.length];
				for (int j = 0; j < annotations.length; j++)
				{
					Annotation annotation = annotations[j];
					Class<? extends Annotation> annotation_class = annotation.annotationType();
					annotation_class_s[j] = annotation_class;
				}
				/**
				 * 这里以后将扩展，将支持更加灵活的注解方式，好几种组合方式， 以便一个 Class 一个 Methed
				 * 一个Field通过的不同的组合，达到不同的意义，实现更加灵活的配置
				 */
				// HashSet<AnnotationWrape> set =
				// AnnotationCombination.Parse(annotation_class_s);
				// for (AnnotationWrape annotationWrape : set)
				// {
				AnnotationWrape annotationWrape = new AnnotationWrape(annotation_class_s);

				ITranslator translator = geTranslator(annotationWrape);

				for (Class<? extends Annotation> class1 : annotationWrape)
				{
					annotationWrape.put(class1, field.getAnnotation(class1));
				}

				if (translator != null)
					translator.execute(field, activity, annotationWrape);
				// }

			}
		}
	}

	/**
	 * 解析方法(Method)的注解
	 * 
	 * @param activity
	 * @param clazz
	 */
	public void initAnotationMethods(IActivity activity, Class<? extends IActivity> clazz)
	{
		Method methods[] = clazz.getDeclaredMethods();// 获取所有的方法
		for (int i = 0; i < methods.length; i++)
		{
			Method method = methods[i];
			Annotation annotations[] = method.getDeclaredAnnotations();
			if (annotations != null)
			{
				@SuppressWarnings("unchecked")
				Class<? extends Annotation> annotation_class_s[] = new Class[annotations.length];
				for (int j = 0; j < annotations.length; j++)
				{
					Annotation annotation = annotations[j];
					Class<? extends Annotation> annotation_class = annotation.annotationType();
					annotation_class_s[j] = annotation_class;
				}
				/**
				 * 这里以后将扩展，将支持更加灵活的注解方式，好几种组合方式， 以便一个 Class 一个 Methed
				 * 一个Field通过的不同的组合，达到不同的意义，实现更加灵活的配置
				 */
				// HashSet<AnnotationWrape> set =
				// AnnotationCombination.Parse(annotation_class_s);
				// for (AnnotationWrape annotationWrape : set)
				// {
				AnnotationWrape annotationWrape = new AnnotationWrape(annotation_class_s);
				ITranslator translator = geTranslator(annotationWrape);

				for (Class<? extends Annotation> class1 : annotationWrape)
				{
					annotationWrape.put(class1, method.getAnnotation(class1));
				}

				if (translator != null)
					translator.execute(method, activity, annotationWrape);
				// }
			}
		}
	}

}
